package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.List;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lanyan.common.utils.DateUtils;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.framework.web.service.TokenService;
import com.lanyan.system.domain.SysLogininfor;
import com.lanyan.system.domain.vo.AccountVo;
import com.lanyan.system.mapper.AccountMapper;
import com.lanyan.system.mapper.SysLogininforMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Account;
import com.lanyan.system.service.IAccountService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 用户Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/account")
public class AccountController extends BaseController {
    @Autowired
    private IAccountService accountService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:account:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "createTimeStart", value = "创建时间-开始", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间-结束", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "昵称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "avatar", value = "头像", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "balance", value = "余额", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "openId", value = "openId", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "vipEnd", value = "vip结束时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),
            @ApiImplicitParam(name = "isActive", value = "是否活跃用户", dataType = "Boolean", dataTypeClass = Boolean.class),

    })
    public TableDataInfo list(Account account) {
        accountService.lambdaUpdate()
                .eq(AccountVo::getDelFlag, "0")
                .le(AccountVo::getVipEnd, new Date())
                .set(AccountVo::getVipEnd, null)
                .update();
        startPage();
        List<Account> list = null;
        list = accountService.selectAccountList(account);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:account:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Account account) {
        List<Account> list = accountService.selectAccountList(account);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:account:query')")
    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId) {
        return success(accountService.selectAccountByAccountId(accountId));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('system:account:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "昵称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "avatar", value = "头像", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "balance", value = "余额", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "openId", value = "openId", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "vipEnd", value = "vip结束时间", dataType = "Date", dataTypeClass = Date.class),
    })
    public AjaxResult add(@RequestBody Account account) {
        return toAjax(accountService.insertAccount(account));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:account:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "昵称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "avatar", value = "头像", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "phone", value = "电话", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "balance", value = "余额", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "openId", value = "openId", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "token", value = "token", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "vipEnd", value = "vip结束时间", dataType = "Date", dataTypeClass = Date.class),
    })
    public AjaxResult edit(@RequestBody Account account) {
        if (StringUtils.equals(account.getIsFreeze(), "1")) {
            Account old = accountService.getById(account.getAccountId());
            tokenService.delLoginUserByToken(old.getToken());
        }
        return toAjax(accountService.updateAccount(account));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:account:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountIds}")
    public AjaxResult remove(@PathVariable Long[] accountIds) {
        return toAjax(accountService.deleteAccountByAccountIds(accountIds));
    }
}
